package yxy.game.pm2.command.shop;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ShopMessage;
import yxy.game.pm2.module.shop.handle.ShopHandle;
import yxy.game.pm2.module.shop.handle.StairwayShopHandle;

/** 商店 天梯商店购买 */
@MessageType(proto = Command.StairwayShopBuy, classtype = ShopMessage.StairwayShopBuyRequest.class)
public class CMD_StairwayShopBuy extends Command<ShopMessage.StairwayShopBuyRequest> {

	@Override
	public void execute(MessageSource messageSource, ShopMessage.StairwayShopBuyRequest request) {
		String playerCode = messageSource.getTag();
		int id = request.getId();
		int num = request.getNum();

		ShopMessage.StairwayShopBuyResponse.Builder response = ShopMessage.StairwayShopBuyResponse.newBuilder();

		try {
			ShopHandle.checkBuyNumber(num);
			Result result = new StairwayShopHandle().buy(playerCode, id, num, response);
			send(messageSource, response, result);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, ShopMessage.StairwayShopBuyResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}